package com.mofofa.maozy.components.path

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import androidx.core.content.ContextCompat
import com.mofofa.maozy.R


class HomeStepPath : View {

    /**
     * 画笔-圆形
     */
    private lateinit var paintCircular: Paint

    /**
     * 画笔-半圆形
     */
    private lateinit var paintHalfCircular: Paint

    /**
     * 颜色-圆形
     */
    private var colorCircular = ContextCompat.getColor(context, R.color.split_line)

    /**
     * 颜色-半圆形
     */
    private var colorHalfCircular = ContextCompat.getColor(context, R.color.main_green)

    /**
     * 宽度-圆形
     */
    private var strokeWidthCircular = 6F

    /**
     * 宽度-半圆形
     */
    private var strokeWidthHalfCircular = 6F

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )

    @SuppressLint("Recycle")
    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int,
    ) : super(
        context,
        attrs,
        defStyleAttr,
        defStyleRes
    ) {
        // 设置画笔
        paintCircular = Paint();
        paintCircular.color = colorCircular;
        paintCircular.isAntiAlias = true
        paintCircular.style = Paint.Style.STROKE
        paintCircular.strokeCap = Paint.Cap.ROUND
        paintCircular.strokeWidth = strokeWidthCircular

        // 设置画笔
        paintHalfCircular = Paint();
        paintHalfCircular.color = colorHalfCircular;
        paintHalfCircular.isAntiAlias = true
        paintHalfCircular.style = Paint.Style.STROKE
        paintHalfCircular.strokeCap = Paint.Cap.ROUND
        paintHalfCircular.strokeWidth = strokeWidthHalfCircular
    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas?) {
        // 画圆环
        val rectFCircular = RectF()
        rectFCircular.left = strokeWidthCircular
        rectFCircular.top = strokeWidthCircular
        rectFCircular.right = width - strokeWidthCircular
        rectFCircular.bottom = width - strokeWidthCircular
        canvas?.drawArc(rectFCircular, 0F, 360F, false, paintCircular)

        // 画半圆环
        val rectFHalfCircular = RectF()
        rectFHalfCircular.left = strokeWidthHalfCircular
        rectFHalfCircular.top = strokeWidthHalfCircular
        rectFHalfCircular.right = width - strokeWidthHalfCircular
        rectFHalfCircular.bottom = width - strokeWidthHalfCircular
        canvas?.drawArc(rectFHalfCircular, 270F, 180F, false, paintHalfCircular)
    }

    /**
     * 可设置背景颜色 跟随一定的规则 变化 重绘
     *
     * @param color 颜色
     */
    fun setPaintCircularColor(color: Int) {
        paintCircular.color = color
        invalidate()
    }

    /**
     * 可设置背景颜色 跟随一定的规则 变化 重绘
     *
     * @param color 颜色
     */
    fun setPaintHalfCircularColor(color: Int) {
        paintHalfCircular.color = color
        invalidate()
    }
}